package com.rzico.sbl.ui.member

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityMemberOfflineRecordBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.OfflineRecord
import com.rzico.sbl.other.showTimeVisitDialog
import com.rzico.sbl.viewmodel.MemberViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.bottomHide
import com.xinnuo.common.helper.bottomShow
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.addOnScrollChanged
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess
import com.xinnuo.slimadapter.SlimAdapter

class MemberOfflineRecordActivity : BaseActivity(R.layout.activity_member_offline_record) {

    private val mBinding by lazy { ActivityMemberOfflineRecordBinding.bind(getRootView()) }

    private var mCardId = ""

    override fun getViewModel() = get<MemberViewModel>()

    override fun initData() {
        initTitle(title = "回访记录")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关回访信息\n 请点击按钮添加回访记录！"
                }

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            if (!isLoadingMore) {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                        }
                    )

                    layout.addItemDecoration(SpaceTBDecoration())
                    layout.addOnScrollChanged(
                        onShow = { mBinding.recordBottom.bottomShow() },
                        onHide = { mBinding.recordBottom.bottomHide(52f) }
                    )

                    mAdapter = SlimAdapter.creator()
                        .register<OfflineRecord>(R.layout.item_offline_record) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_record_time, bean.visitDate.getTimeString())
                                text(R.id.item_record_name, bean.operator.orEmpty())
                                text(R.id.item_record_memo, bean.memo)
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

    override fun initListener() {
        mCardId = intent.getExtra("cardId")

        mBinding
            .recordAdd
            .oneClick {
                showTimeVisitDialog { date, memo ->
                    getViewModel()
                        .requestBind(
                            requestUrl = BaseUrl.mngVisitAdd,
                            isMultipart = true,
                            params = arrayOf(
                                "id" to mCardId,
                                "visitDate" to "$date:00",
                                "memo" to memo
                            )
                        )
                        .bindLifeCycle(getLifecycleOwner())
                        .subscribeBySuccess(
                            event = getViewModel(),
                            onSuccess = {
                                showToast("添加回访成功！")
                                updateList()
                            }
                        )
                }
            }
    }

    /**
     * 休眠会员列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .visitList(
                page = index,
                cardId = mCardId,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}